package be.aquafin.klipdf.handlers;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;

import be.aquafin.klipdf.dao.DataDao;
import be.aquafin.klipdf.model.DataObject;
import be.aquafin.klipdf.model.SpatialObject;
import be.aquafin.klipdf.model.json.JSONObject;

public abstract class DataHandler {
	
	protected DataDao dataDao;
	
	protected DataHandler(DataDao dataDao) {
		this.dataDao = dataDao;
	}
	
	public JSONArray process(List<? extends SpatialObject> spatialObjects) {
		JSONArray jsonArray = new JSONArray();
		List<String> keys = getKeys(spatialObjects);

		List<? extends DataObject> dataObjects = getData(keys);
		for (DataObject dataObject: dataObjects) {
			JSONObject jsonObject = process(dataObject);
			jsonArray.put(jsonObject);
		}

		return jsonArray;
	}
	
	protected abstract List<? extends DataObject> getData(List<String> keys);

	protected abstract JSONObject process(DataObject dataObject);
	
	private List<String> getKeys(List<? extends SpatialObject> spatialObjects) {
		List<String> keys = new ArrayList<String>();
		for (int i = 0; i < spatialObjects.size(); i++) {
			keys.add(spatialObjects.get(i).getKey());                                 
		}
		return keys;
	}

}
